<?php
/*---------------------------------------------
常用通用方法
---------------------------------------------*/
error_reporting(E_ERROR);

include_once('../../config/dao.cfg.php');
function exec_out($cmd,$stdout=true)
{
	$text="";
	exec($cmd,$output,$result);
	foreach($output as $key=>$value)
	{
		$text.="$value\n";
		if($stdout)
		echo $value."\n";
	}
	return $text;
}

function get_client_ip()
{
	return $_SERVER['REMOTE_ADDR'];
}

function dao_str_replace($search,$rep,&$value)
{
	//先strpos，检查是否有需要替换的，再替换，速度会快很多，因为str_replace先分配空间，不管是否有替换
	if(strpos($value,$search)===false)
	{
		//nothing
	}
	else
	{
		$value = str_replace($search,$rep,$value);
	}
	return $value;
}

function adjust_path($path)
{
	$path = trim($path,"\\");
	$path = trim($path,"/");
	$path.="/";
	return $path;
}

function snap_time()
{
	list($usec, $sec) = explode(' ', microtime());
	$timesnapshot= (((float)$usec + (float)$sec%1000))*1000;
	return $timesnapshot;
}

//参数$date_or_time 0:返回标准日期,1:返回标准时间,2:返回标准日期加时间
function get_std_time_string($time=0,$date_or_time=2)
{
	if($time==0)
	{
		$time=time();
	}
	$time_string = date('H:i:s',$time);
	$date_string = date('Y-m-d',$time);
	if($date_or_time==0)
	{
		return $date_string;
	}
	if($date_or_time==1)
	{
		return  $time_string;
	}
	return "$date_string $time_string";
}


function my_strptime($time_string,$format,$use_strptime=1)
{
	if(function_exists('strptime')&&$use_strp_time)
	{
		return strptime($time_string,$format);
	}
	else
	{
		$format = dao_str_replace("%Y","%04d",$format);
		$format = dao_str_replace("%m","%02d",$format);
		$format = dao_str_replace("%d","%02d",$format);
		$format = dao_str_replace("%H","%02d",$format);
		$format = dao_str_replace("%M","%02d",$format);
		$format = dao_str_replace("%H","%02d",$format);
		$format = dao_str_replace("%S","%02d",$format);
		$result = sscanf($time_string,$format);
		$tm['tm_year']=$result[0]-1900;
		$tm['tm_mon']=$result[1]-1;
		$tm['tm_day']=$result[2];
		$tm['tm_hour']=$result[3];
		$tm['tm_min']=$result[4];
		$tm['tm_sec']=$result[5];
		return $tm;
	}
}

//输入YYYY-MM-DD HH:mm:ss,
//输出 $tm["tm_hour"],$tm["tm_min"],$tm["tm_sec"],$tm["tm_mon"],$tm["tm_day"],$tm["tm_year"]的数组
//兼容YYYYMMDD的日期格式
//兼容YYYYMM的日期格式
function std_strptime($time_string)
{
	$len = strlen($time_string);
	if($len==strlen('YYYYmm'))
	{
		$tm= my_strptime($time_string,"%Y%m");
		return $tm;
	}
	else if($len==strlen("YYYYmmdd"))
	{
		return my_strptime($time_string,"%Y%m%d");
	}
	else if($len==strlen("YYYY-mm-dd"))
	{
		return   my_strptime ($time_string,"%Y-%m-%d");
	}
	else if($len=strlen("YYYY-mm-yy hh:mi:ss"))
	{
		return   my_strptime ($time_string,"%Y-%m-%d %H:%M:%S");
	}
	return NULL;
}

//输入YYYY-MM-DD HH:mm:ss，输出1970年以来的秒数
function std_string_to_time($time_string)
{
	$tm =  std_strptime($time_string);
	if($tm==NULL)
	{
		return -1;
	}
	$t = mktime($tm['tm_hour'],$tm['tm_min'],$tm['tm_sec'],$tm['tm_mon']+1,$tm['tm_mday'],$tm['tm_year']);
	return $t;
}

//传入"YYYY-MM-DD HH:M:S"格式的开始和结束时间，找出中间共有多少个月
//result格式，数组，每个数组 year=>年，month=>月
function get_months_between(&$result,$start_time_string,$end_time_string,$allow_month=0)
{
	$result=array();
	if(strlen($start_time_string)==0&&strlen($end_time_string)==0)//如果两者都没有设置，则返回
	{
		return 0;
	}

	if(strlen($end_time_string)==0)
	{
		//未传入结束时间,从开始时间推到现在
		$end_time_string = get_std_time_string(time()+60);//增加60s
		//将时间换算成tm格式,便于获取年月
		$start_time = std_strptime($start_time_string);
		$end_time = std_strptime($end_time_string);
		if($start_time<0||$end_time<0)
		{
			return -1;
		}
		$year = $start_time['tm_year'];
		$mon  = $end_time['tm_mon'];
		$i = 0;
		$finish_year = $end_time['tm_year'];
		$finish_mon = $end_time['tm_mon'];
		while($year<=$finish_year)
		{
			$result []=array('year'=>(1900+$year),'mon'=>($mon+1));
			$mon ++;

			if($mon>=12)
			{
				$mon=0;
				$year++;
			}
			if($year==$finish_year&&$mon>$finish_mon)
			{
				break;
			}
		}
		return ;
	}

	if(strlen($start_time_string)==0)
	{
		//未传入开始时间，赋一个初始时间，不超过2000-01-01
		$start_time_string = "2000-01-01 00:00:00";
	}

	//---------------------------------
	//start_time_string和end_time_string都已经设置，则从endtime回退，如果$allow_month大于0，则作限制
	$start_time = std_strptime($start_time_string);
	$end_time = std_strptime($end_time_string);
	if($start_time<0||$end_time<0)
	{
		return -1;
	}
	$i = 0;
	//回退时以start_time为终止计数时间
	$finish_year = $start_time['tm_year'];
	$finish_mon  = $start_time['tm_mon'];
	$year = $end_time['tm_year'];
	$mon = $end_time['tm_mon'];
	$counter=0;
	while( $year>=$finish_year )
	{
		$result []=array('year'=>(1900+$year),'mon'=>($mon+1));
		$counter++;
		if($allow_month>0)
		{
			//如果$allowMonth大于0，则作限制
			if($counter>=$allow_month)
			{
				break;
			}
		}
		$mon--;
		if($mon<0)
		{
			$mon=11;
			$year--;
		}
		
		if( $year==$finish_year&&$mon<$finish_mon)
		{
			break;
		}
	}
}

//输入YYYY-MM-DD HH:MI:SS 格式两个时间比较，
//0，相等
//<0 ,first早于second
//>0,first晚于second
function compare_time_string($first,$second)
{
	$f =  std_string_to_time($first);
	$s =  std_string_to_time($second);
	return $f-$s;
}

//in是包含YYYY-MM-DD HH:MI:SS时间格式的数组，
//这个函数的用意是找出来在这个数组里最早和最晚的时间
function get_time_term($in ,&$begin,&$end)
{
	$begin =$in[0];
	$end = $in[0];

	foreach ($in as $key=>$value)
	{
		if(strlen($value)==0)
		{
			continue;
		}
		if(compare_time_string($value,$begin)<0)
		{
			$begin = $value;
		}
		if(compare_time_string($value,$end)>0)
		{
			$end= $value;
		}
	}
}

function is_time_between($time_string,$begin,$end)
{
	if(compare_time_string($time_string,$begin)>=0&&compare_time_string($time_string,$end)<=0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function get_pid()
{
	if(PHP_OS=="WINNT")
	{
		return 0;
	}
	else
	{
		return posix_getgid();
	}
}

//返回20位的sn
function get_std_sn()
{
	static $sn=0;
	$max = 1000000;
	$sn++;
	if($sn>=$max)
	{
		$sn=0;
	}
	$strsn = sprintf("%010d%04d%06d",time()%10000000000,get_pid()%10000,$sn);
	return $strsn;
}

function vexport(&$var,$quot=NULL,$compact=false)
{
	if($compact)
	{
		if(is_object($var))
		{
			return get_class($var);
		}
		if(is_array($var))
		{
			return 'Array['.sizeof($var).']';
		}
		return "".($var);
	}
	if(isset($quot))
	{
		$str= dao_str_replace(CRLF,$quot,var_export($var,true));
	}
	else
	$str = var_export($var,true);
	return $str;

}

//将数组输出成简单文本，每个字段用$quot分隔，每个回车换行用$crlf代替
function array_export(&$var,$quot="|",$crlf="\\n")
{
	$urlquot = urlencode($quot);
	$urlequal = urlencode("=");
	$text = "";
	foreach($var as $key=>$value)
	{
		$value=  dao_str_replace($quot,$urlquot,$value);
		$value = dao_str_replace("\r","",$value);
		$value = dao_str_replace("\n",$crlf,$value);
		$value = dao_str_replace("=",$urlequal,$value);
		$text.=$key."=".$value.$quot;
	}
	return $text;
}

//对应array_export的解析
function array_parse(&$content,&$data,$quot="|",$crlf="\\n")
{
	$varray = split("\|",$content);
	$urlquot = urlencode($quot);
	$urlequal = urlencode("=");
	foreach ($varray as $n=>$v)
	{
		$kv = split("=",$v);
		$t_key = trim($kv[0]);
		$t_value   = trim($kv[1]);
		if(strlen($t_key)==0||strlen($t_value)==0) 
		{
			continue;
		}
		$t_value = dao_str_replace('\\n',"\n",$t_value);
		$t_value = dao_str_replace('\\r',"\r",$t_value);
		$t_value = dao_str_replace($urlquot,$quot,$t_value);
		$value = dao_str_replace($urlequal,"=",$t_value);
		$data[$t_key] = $t_value;
	}
}

function is_qq($qq)
{
	if(!is_numeric($qq))
	{
		return false;
	}
	if($qq<=10000)
	{
		return false;
	}
	if(strlen($qq)>16)
	{
		return false;
	}
	return true;
}

global $qq_errkey;
$qq_errkey=array("8888","12345","11111","10000","00000");
function check_qq($qq)
{
	if(!is_qq($qq))
	{
		return false;
	}
	global $qq_errkey;
	foreach ($qq_errkey as $key=>$value)
	{
		if(strcmp($qq,$value)!=0)
		{
			continue;
		}
		return false;
	}
	return true;
}

static $class_counter=array();
function class_counter_dump()
{
	global $class_counter;
	return vexport($class_counter);
}

function class_counter_add($object)
{

	global $class_counter;
	$name = get_class($object);
	if(strlen($name)==0)
	{
		return;
	}
	$class_counter[$name]['add']++;
}

function class_counter_dec($object)
{
	global $class_counter;
	$name = get_class($object);
	if(strlen($name)==0)
	{
		return;
	}
	$class_counter[$name]['dec']++;
}

function ut($name,$argv)
{
	$func = $name;
	echo "UNIT TEST: $func start....\r\n ";
	$start = snap_time();
	$ret = $func($argv,$msg);
	$stop = snap_time();
	echo "UNIT TEST:$func use time(ms) = ".($stop-$start).";ret=$ret,msg=$msg\r\n";
}

function date_by_id($id)
{
	if(strlen($id)<6)
	{
		return "";
	}
	$date_string = substr($id,0,6);
	$date = std_strptime($date_string);
	if($date==NULL)//不正确的时间日期
	{
		return '';
	}
	return $date_string;
}

function array_to_hash(&$array,&$hash,$key_fields)
{
	$hash=array();
	foreach($array as $dkey=>$value)
	{
		$key = "";
		foreach($key_fields as $k=>$v)
		{
			$key=$value[$v].":";
		}
		$hash[$key]=$value;
	}
}

function hexstr($str)
{
	$buffer="";
	$str=bin2hex($str);
	for($i=0,$len=strlen($str);$i<$len;$i+=2)
	{
		$buffer.=substr($str,$i,2)." ";
		$d=$i+2;
		if(($d%38) ==0)$buffer.="\r\n";
		//	else
		//	if(($d%16) ==0)$buffer.=" ";
	}
	return $buffer;
}

?>
